<?php

namespace app\http\middleware;

use app\helper\TokenHelper;
use app\util\ReturnCode;

class AppToken {

    /**
     * token鉴权
     * @param \think\facade\Request $request
     * @param \Closure $next
     * @return mixed|\think\response\Json
     */
    public function handle($request, \Closure $next) {
//        $header = config('apiadmin.CROSS_DOMAIN');
        $ApiAuth = $request->header('apiAuth', '');
        if ($ApiAuth) {
            $userInfo = TokenHelper::getUserByToken($ApiAuth);
            if (!$userInfo || !isset($userInfo['uid'])) {
                return json([
                    'code' => ReturnCode::AUTH_ERROR,
                    'msg'  => 'ApiAuth不匹配',
                    'data' => []
                ]);
            } else {
                $request->APP_USER_INFO = $userInfo;
            }

            return $next($request);
        } else {
            return json([
                'code' => ReturnCode::AUTH_ERROR,
                'msg'  => '缺少ApiAuth',
                'data' => []
            ]);
        }
    }
}
